﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using System.Threading;
using System.Drawing;

namespace MediaCenter.Controls
{
    public class LCARS_Form : Form
    {
        private Device DX_DEV = null;
        private Microsoft.DirectX.Direct3D.Font dx_text;
        private Thread th_render;
        private System.Windows.Forms.Timer _FSPTimer;
        private RenderMode _rendermode;

        public RenderMode RenderMode
        {
            get
            {
                return _rendermode;
            }
            set
            {
                _rendermode = value;
            }
        }

        public Device D3X_Dev
        {
            get
            {
                return DX_DEV;
            }
        }

        public LCARS_Form()
        {
            this.Load += new EventHandler(LCARS_Form_Load);
            this.FormClosing += new FormClosingEventHandler(LCARS_Form_FormClosing);
        }

        void LCARS_Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            isRendering = false;
            if (DX_DEV != null)
                DX_DEV.Dispose();
        }

        void LCARS_Form_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode && _rendermode == RenderMode.DirectX)
            {
                this._FSPTimer = new System.Windows.Forms.Timer();
                this._FSPTimer.Enabled = true;
                this._FSPTimer.Interval = 1000;
                this._FSPTimer.Tick += new System.EventHandler(this._FSPTimer_Tick);

                CreateDirectXDevice();
            }
        }
        private bool isRendering;
        private void CreateDirectXDevice()
        {
            PresentParameters DX_DEV_Param = new PresentParameters();
            DX_DEV_Param.Windowed = true;
            DX_DEV_Param.SwapEffect = SwapEffect.Discard;
            DX_DEV = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, DX_DEV_Param);
            //Render3Eck();
            System.Drawing.Font systemfont = new System.Drawing.Font("Arial", 12f, FontStyle.Regular);
            fps = 0;
            dx_text = new Microsoft.DirectX.Direct3D.Font(DX_DEV, systemfont);
            isRendering = true;
            th_render = new Thread(new ThreadStart(Render));
            th_render.Start();
        }

        private void Render()
        {
            while (isRendering)
            {
                try
                {
                    fpszähl++;
                    DX_DEV.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0);
                    DX_DEV.BeginScene();

                    foreach (Control c in this.Controls)
                    {
                        LCARS_Control lc = c as LCARS_Control;
                        if (lc != null)
                            lc.Render();
                    }


                    //dx_text.DrawText(null, "FSP: " + fps.ToString(), new Point(30, 20), Color.White);
                    DX_DEV.EndScene();
                    DX_DEV.Present();
                }
                catch
                { }
            }
        }

        private int fps;
        private int fpszähl;
        private void _FSPTimer_Tick(object sender, EventArgs e)
        {
            fps = fpszähl;
            fpszähl = 0;
        }


    }
}
